#include "recordlist.h"

using namespace std;

RecordList::RecordList(){

}
void RecordList::addRecord(Record r)
{
    RCL.push_back(r);
}

void RecordList::deleteRecord(Record r)
{
    for(unsigned int i=0;i<RCL.size();++i)
    {
        if(RCL[i].getnameid() == r.getnameid())
        {
            RCL.erase(RCL.begin()+i);
            break;
        }
    }
}

void RecordList::modifyRecord(Record oldRecord,Record newRecord)
{
    //find the old Record
    for (unsigned int i=0;i<RCL.size();i++)
    {
        if(RCL[i].getnameid()==oldRecord.getnameid())
        {
            //insert the new Record
            RCL[i] = newRecord;
            //break loop
            break;
        }
    }

}
vector<Record> RecordList::searchRecord(string input, string category, string field,
                                        QDateTime fromTime, QDateTime toTime, double fromPrice, double toPrice){
    int i = 0;
    vector<Record> results;
    if(field == "All Fields"){
        if(category == "All")
            for(i = 0; i < RCL.size(); i++){
              if(RCL[i].getinfo().find(input) != string::npos || RCL[i].getname().find(input) != string::npos ||
                      (RCL[i].getamount() < toPrice && RCL[i].getamount() > fromPrice) ||
                      (RCL[i].getdatetime() < toTime && RCL[i].getdatetime() > fromTime))
                  results.push_back(RCL[i]);
            }
        else{
            for(i = 0; i < RCL.size(); i++){
              if(RCL[i].getcategory() == category)
                  if(RCL[i].getinfo().find(input) != string::npos || RCL[i].getname().find(input) != string::npos||
                          (RCL[i].getamount() < toPrice && RCL[i].getamount() > fromPrice) ||
                          (RCL[i].getdatetime() < toTime && RCL[i].getdatetime() > fromTime))
                      results.push_back(RCL[i]);
            }
        }
     }
     else if(field == "Name"){
        if(category == "All"){
            for(i = 0; i < RCL.size(); i++)
               if(RCL[i].getname().find(input) != string::npos)
                   results.push_back(RCL[i]);
        }
        else{
            for(i = 0; i < RCL.size(); i++)
                  if(RCL[i].getcategory() == category)
                        if(RCL[i].getname().find(input) != string::npos)
                              results.push_back(RCL[i]);
        }
     }
     else if(field == "Description"){
        if(category == "All"){
            for(i = 0; i < RCL.size(); i++)
                    if(RCL[i].getinfo().find(input) != string::npos)
                       results.push_back(RCL[i]);
         }
        else{
            for(i = 0; i < RCL.size(); i++)
                if(RCL[i].getcategory() == category)
                    if(RCL[i].getinfo().find(input) != string::npos)
                        results.push_back(RCL[i]);
        }
    }
    else if(field == "Price Range"){
        if(category == "All"){
            for(i = 0; i < RCL.size(); i++)
                    if(RCL[i].getamount() < toPrice && RCL[i].getamount() > fromPrice)
                       results.push_back(RCL[i]);
         }
        else{
            for(i = 0; i < RCL.size(); i++)
                if(RCL[i].getcategory() == category)
                    if(RCL[i].getamount() < toPrice && RCL[i].getamount() > fromPrice)
                        results.push_back(RCL[i]);
        }
    }
    else if(field == "Date Range"){
        if(category == "All"){
            for(i = 0; i < RCL.size(); i++)
                    if(RCL[i].getdatetime() < toTime && RCL[i].getdatetime() > fromTime)
                       results.push_back(RCL[i]);
         }
        else{
            for(i = 0; i < RCL.size(); i++)
                if(RCL[i].getcategory() == category)
                    if(RCL[i].getdatetime() < toTime && RCL[i].getdatetime() > fromTime)
                        results.push_back(RCL[i]);
        }
    }
    return results;
}
vector<Record> RecordList::getRecordList(void)
{
    return RCL;
}
void RecordList::clearList()
{
    RCL.clear();
}
int RecordList::size(){
    return RCL.size();
}
void RecordList::printList(){
    int i = 0;
    for(i = 0;i <RCL.size();i++){
        RCL[i].print();
    }
}
Record RecordList::operator [](int index){
    return RCL[index];
}
